package liujia.bt.display.core
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import liujia.bt.display.BtDisplay;
	import liujia.bt.display.interf.IBtMouse;
	import liujia.bt.display.interf.IBtMovieClip;
	
	/**
	 * BtRun
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtRun.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtRun
	{
		/**
		 *舞台 
		 */		
		public static var stage:Stage;
		
		/**
		 *从flashplayer开始运行现在的毫秒数 
		 */		
		public static var nowTime:int;
		
		/**
		 *鼠标在舞台上的坐标 
		 */		
		public static var mousePoint:Point = new Point();
		
		/**
		 *鼠标在最近一帧内X轴滑动的像素 
		 */		
		public static var mouseMoveX:Number = 0;
		
		/**
		 *鼠标在最近一帧内Y轴滑动的像素 
		 */		
		public static var mouseMoveY:Number = 0;
		
		/**
		 *上一帧的鼠标在舞台上的坐标，用于计算鼠标的滑动像素 
		 */		
		private static var oldMousePoint:Point = new Point();
		
		/**
		 *正在播放的位图动画的播放列表
		 */		
		private static var btMcVec:Vector.<IBtMovieClip> = new Vector.<IBtMovieClip>;

		/**
		 *正在播放的有鼠标事件功能的位图动画的播放列表
		 */		
		private static var btMouseVec:Vector.<IBtMouse> = new Vector.<IBtMouse>;
		
		/**
		 *初始化 
		 * @param $stage 舞台
		 * 
		 */		
		public static function init($stage:Stage):void {
			stage = $stage;
			stage.doubleClickEnabled = true;
			nowTime = getTimer();
			stage.addEventListener(Event.ENTER_FRAME, onFrame);
			addMouseEvents();
		}
		
		/**
		 *Event.ENTER_FRAME触发的方法 
		 * @param e
		 * 
		 */		
		private static function onFrame(e:Event):void {
			oldMousePoint.x = mousePoint.x;
			oldMousePoint.y = mousePoint.y;
			mousePoint.x = stage.mouseX;
			mousePoint.y = stage.mouseY;
			mouseMoveX = mousePoint.x - oldMousePoint.x;
			mouseMoveY = mousePoint.y - oldMousePoint.y;
			
			nowTime = getTimer();
			for each (var btMc:IBtMovieClip in btMcVec) {
				btMc.enterFrame();
			}
		}
		
//		/**
//		 *舞台上的鼠标事件 
//		 * @param e
//		 * 
//		 */		
//		private static function onMouse(e:MouseEvent):void {
//			removeMouseEvents();
//			for each (var btMc:IBtMovieClip in btMcVec) {
//				if (btMc is IBtMouse) {
//					(btMc as IBtMouse).setMouseEvent(e);
//				}
//			}
//			addMouseEvents();
//		}
		
		/**
		 *舞台上的鼠标事件 
		 * @param e
		 * 
		 */		
		private static function onMouse(e:MouseEvent):void {
			removeMouseEvents();
			var arr:Array = stage.getObjectsUnderPoint(mousePoint);
			
			for each (var btMc:IBtMouse in btMouseVec) {
				if (arr.indexOf(btMc) == -1) { //对象根本不在鼠标坐标下
					btMc.setMouseEvent(null, false, false);
				}
			}
			
			var isMouseHit:Boolean = true;
			for (var i:int=arr.length-1; i>=0; i--) {
				btMc = arr[i] as IBtMouse;
				if (btMc) {
					var isPointHit:Boolean = btMc.isPointHit();
					if (isPointHit) {
						btMc.setMouseEvent(e, isMouseHit, true);
						isMouseHit = false;
					} else {
						btMc.setMouseEvent(e, isMouseHit&&isPointHit, false);
					}
				}
			}
			addMouseEvents();
		}
		
		/**
		 *添加舞台鼠标事件 
		 * 
		 */		
		private static function addMouseEvents():void {
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouse);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouse);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouse);
			stage.addEventListener(MouseEvent.CLICK, onMouse);
			stage.addEventListener(MouseEvent.DOUBLE_CLICK, onMouse);
		}
		
		/**
		 *移除舞台鼠标事件 
		 * 
		 */		
		private static function removeMouseEvents():void {
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouse);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouse);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouse);
			stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouse);
			stage.removeEventListener(MouseEvent.CLICK, onMouse);
			stage.removeEventListener(MouseEvent.DOUBLE_CLICK, onMouse);
		}
		
		/**
		 *把位图动画添加到播放列表 
		 * @param btMc
		 * 
		 */		
		public static function addBtMovieClip(btMc:IBtMovieClip):void {
			if (btMcVec.indexOf(btMc) == -1) {
				btMcVec.push(btMc);
				if (btMc is IBtMouse) btMcVec.push(btMc);		
			}
		}
		
		/**
		 *把位图动画从播放列表移除 
		 * @param btMc
		 * 
		 */
		public static function removeBtMovieClip(btMc:IBtMovieClip):void {
			var index:int = btMcVec.indexOf(btMc);
			if (index != -1) {
				btMcVec[index] = btMcVec.pop();
			}
			
			if (btMc is IBtMouse) {
				index = btMouseVec.indexOf(btMc);
				if (index != -1) {
					btMouseVec[index] = btMouseVec.pop();
				}
			}
		}
	}
}
